var board = new Array();
var added = new Array();
var score = 0;
var top = 240;
$(function () {
    newgame();
});

function newgame() {
    init();
    generateOneNumber();
    generateOneNumber();
}

// 初始化游戏
function init() {
    score = 0;
    $("#score").html(score);
    $("gameover").css("display", "none");

    //初始化格子位置
    for (let i = 0; i < 4; i++) {
        for (let j = 0; j < 4; j++) {
            var girdCell = $("#grid-cell-" + i + "-" + j)
            girdCell.css("top", getPosTop(i, j)).css("left", getPosLeft(i, j));
        }
    }

    //初始化格子数组
    for (let i = 0; i < 4; i++) {
        board[i] = new Array();
        for (let j = 0; j < 4; j++) {
            board[i][j] = 0;
        }
    }

    //初始化判断合并的数组
    for (let i = 0; i < 4; i++) {
        added[i] = new Array;
        for (let j = 0; j < 4; j++) {
            added[i][j] = 0;
        }
    }

    updateBoardView();
}

//更新数组的样式
function updateBoardView() {
    $(".number-cell").remove();
    for (let i = 0; i < 4; i++) {
        for (let j = 0; j < 4; j++) {
            $("#grid-container").append("<div class='number-cell' id='number-cell-" + i + "-" + j + "'></div>")
            var theNumberCell = $("#number-cell-" + i + "-" + j);
            if (board[i][j] == 0) {
                theNumberCell.css({
                    width: 0,
                    height: 0,
                    top: getPosTop(i, j),
                    left: getPosLeft(i, j)
                })
            } else {
                theNumberCell.css({
                    width: 100,
                    height: 100,
                    top: getPosTop(i, j),
                    left: getPosLeft(i, j),
                    backgroundColor: getNumberBackgroundColor(board[i][j]),
                    color: getNumberColor(board[i][j])
                });
                theNumberCell.text(board[i][j]);
            }
        }
    }
}

//生成随机格子
function generateOneNumber(dir) {
    if (nospace(board)) {
        return false;
    }
    var randx = 0;
    var randy = 0;
    function randxy() {
        randx = parseInt(Math.floor(Math.random() * 4));
        randy = parseInt(Math.floor(Math.random() * 4));
        switch (dir) {
            case 37:
                randy = 3;
                break;
            case 38:
                randx = 3;
                break;
            case 39:
                randy = 0;
                break;
            case 40:
                randx = 0;
                break;
        }
    }

    //随机位置
    randxy();
    while (true) {
        if (board[randx][randy] == 0) {
            break;
        }
        randxy();
    }

    // var randNumber = Math.random() < 0.5 ? 2 : 4;
    var randNumber = 4;
    board[randx][randy] = randNumber;
    showNumberWithAnimation(randx, randy, randNumber);
    return true;
}
var reboard;
var rescore;
$(document).keydown(function (event) {
    reboard = JSON.parse(JSON.stringify(board));
    rescore = score;
    switch (event.keyCode) {
        case 37:
            if (moveLeft()) {
                getScore();
                generateOneNumber(37);
                setTimeout(isgameover, 400);
            }
            break;
        case 38:
            if (moveUp()) {
                getScore();
                generateOneNumber(38);
                setTimeout(isgameover, 400);
            }
            break;
        case 39:
            if (moveRight()) {
                getScore();
                generateOneNumber(39);
                setTimeout(isgameover, 400);
            }
            break;
        case 40:
            if (moveDown()) {
                getScore();
                generateOneNumber(40);
                setTimeout(isgameover, 400);
            }
            break;
    }
})

function isgameover() {
    if (nospace(board) && nomove(board)) {
        gameover();
    }
}

function gameover() {
    $("#gameover").css('display', 'flex');
}

function isaddedArray() {
    for (let i = 0; i < 4; i++) {
        for (let j = 0; j < 4; j++) {
            added[i][j] = 0;
        }
    }
}

function moveLeft() {
    if (!canMoveLeft(board)) {
        return false;
    }

    isaddedArray();

    for (let i = 0; i < 4; i++) {
        for (let j = 1; j < 4; j++) {
            if (board[i][j] != 0) {
                for (let k = 0; k < j; k++) {
                    if (board[i][k] == 0 && noBlockHorizontal(i, k, j, board)) {
                        showMoveAnimation(i, j, i, k);
                        board[i][k] = board[i][j];
                        board[i][j] = 0;
                        continue;
                    } else if (board[i][k] == board[i][j] && noBlockHorizontal(i, k, j, board)) {
                        showMoveAnimation(i, j, i, k);
                        if (added[i][k] != 0) {
                            board[i][k + 1] = board[i][j];
                            board[i][j] = 0;
                        }
                        else {
                            board[i][k] += board[i][j];
                            board[i][j] = 0;
                            added[i][k] = 1;
                            score += board[i][k];
                        }
                        continue;
                    }
                }
            }
        }
    }
    setTimeout(updateBoardView, 200);
    return true;
}
function moveRight() {
    if (!canMoveRight(board)) {
        return false;
    }

    isaddedArray();

    for (let i = 0; i < 4; i++) {
        for (let j = 2; j >= 0; j--) {
            if (board[i][j] != 0) {
                for (let k = 3; k > j; k--) {
                    if (board[i][k] == 0 && noBlockHorizontal(i, j, k, board)) {
                        showMoveAnimation(i, j, i, k);
                        board[i][k] = board[i][j];
                        board[i][j] = 0;
                        continue;
                    } else if (board[i][k] == board[i][j] && noBlockHorizontal(i, j, k, board)) {
                        showMoveAnimation(i, j, i, k);
                        if (added[i][k] != 0) {
                            board[i][k - 1] = board[i][j];
                            board[i][j] = 0;
                        }
                        else {
                            board[i][k] += board[i][j];
                            board[i][j] = 0;
                            added[i][k] = 1;
                            score += board[i][k];
                        }
                        continue;
                    }
                }
            }
        }
    }
    setTimeout(updateBoardView, 200);
    return true;
}
function moveUp() {
    if (!canMoveUp(board)) {
        return false;
    }

    isaddedArray();

    for (let j = 0; j < 4; j++) {
        for (let i = 1; i < 4; i++) {
            if (board[i][j] != 0) {
                for (let k = 0; k < i; k++) {
                    if (board[k][j] == 0 && noBlockVertical(j, k, i, board)) {
                        showMoveAnimation(i, j, k, j);
                        board[k][j] = board[i][j];
                        board[i][j] = 0;
                        continue;
                    } else if (board[k][j] == board[i][j] && noBlockVertical(j, k, i, board)) {
                        showMoveAnimation(i, j, i, k);
                        if (added[k][j] != 0) {
                            board[k + 1][j] = board[i][j];
                            board[i][j] = 0;
                        }
                        else {
                            board[k][j] += board[i][j];
                            board[i][j] = 0;
                            added[k][j] = 1;
                            score += board[k][j];
                        }
                        continue;
                    }
                }
            }
        }
    }
    setTimeout(updateBoardView, 200);
    return true;
}
function moveDown() {
    if (!canMoveDown(board)) {
        return false;
    }

    isaddedArray();

    for (let j = 0; j < 4; j++) {
        for (let i = 2; i >= 0; i--) {
            if (board[i][j] != 0) {
                for (let k = 3; k > i; k--) {
                    if (board[k][j] == 0 && noBlockVertical(j, i, k, board)) {
                        showMoveAnimation(i, j, k, j);
                        board[k][j] = board[i][j];
                        board[i][j] = 0;
                        continue;
                    } else if (board[k][j] == board[i][j] && noBlockVertical(j, i, k, board)) {
                        showMoveAnimation(i, j, k, j);
                        if (added[k][j] != 0) {
                            board[k - 1][j] = board[i][j];
                            board[i][j] = 0;
                        }
                        else {
                            board[k][j] += board[i][j];
                            board[i][j] = 0;
                            added[k][j] = 1;
                            score += board[k][j];
                        }
                        continue;
                    }
                }
            }
        }
    }
    setTimeout(updateBoardView, 200);
    return true;
}

function back() {
    board = reboard;
    score = rescore;
    getScore();
    updateBoardView();
}